/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2022-07-15
 * Time:21:16
 */

import java.util.*;


 class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
public class PalindromeList {
    public static void main(String[] args) {
        ListNode node = new ListNode(12);
        ListNode node1 = new ListNode(13);
        ListNode node2 = new ListNode(12);
        System.out.println(chkPalindrome(node));

    }
    public static boolean chkPalindrome(ListNode head) {
        if(head == null) {
            return true;
        }
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        //反转
        ListNode cur = slow.next;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        //开始判断回文
        while (head != slow) {
            if(head.val != slow.val) {
                return false;
            }
            if(head.next == slow) {
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }
}